from fastapi import FastAPI, WebSocket, WebSocketDisconnect, BackgroundTasks
from langchain_openai import ChatOpenAI
from langchain.agents import create_openai_tools_agent, AgentExecutor, tool
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.schema import StrOutputParser
from langchain.memory import ConversationTokenBufferMemory
from langchain_community.chat_message_histories import RedisChatMessageHistory
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
import os
import asyncio
import uuid
from Mytools import *
os.environ["LANGCHAIN_TRACING_V2"]="false"
os.environ["LANGCHAIN_API_KEY"]="ls__a339aed7ea934ec999736421cdb43629"
os.environ["LANGCHAIN_PROJECT"]="samibot"

os.environ["OPENAI_API_KEY"] = "sk-DDvLuNQngcoW3y8V982b799e01F94d45B0Cb0eEd42Ae3fBb"
os.environ["OPENAI_API_BASE"] = "https://ai-yyds.com/v1"

os.environ["SERPAPI_API_KEY"] = "e7b15718df583476214651b32a4e835523f3bac273d511341a1de30430b2164e"
REDIS_URL = "redis://:@127.0.0.1:6379" #os.getenv("REDIS_URL")


app = FastAPI()

@app.post("/chat")
def chat(query: str, context: str, background_tasks: BackgroundTasks):

    unique_id = str(uuid.uuid4())  # 生成唯一的标识符
    return {"msg": query, "id": unique_id}

@app.get("/")
def read_root():
    return {"Hello": "World"}
@app.post("/add_pdfs")
def add_pdfs():
    return {"response": "PDFs added!"}

@app.post("/add_texts")
def add_texts():
    return {"response": "Texts added!"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8001)
